package util;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import pojo.PortAndServlet;
import server.HttpServlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommonUtil {
    /**
     * 根据web.xml的路径，解析web.xml
     * @param inputStream web.xml的流路径
     * @return
     */
    private static Map<String,HttpServlet> servletMap = new HashMap<>();
    private static Map<String,HttpServlet> loadServlet(InputStream inputStream,String host){
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(inputStream);
            Element rootElement = document.getRootElement();
            List<Element> servletNodes = rootElement.selectNodes("//servlet");
            for (int i = 0; i < servletNodes.size(); i++) {
                Element element = servletNodes.get(i);
                Element servletNameElement = (Element) element.selectSingleNode("servlet-name");
                String servletName = servletNameElement.getStringValue();//lagou
                Element servletClassElement = (Element) element.selectSingleNode("servlet-class");
                String servletClass = servletClassElement.getStringValue();//server.LaogoServlet

                //根据servlet-name的值找到url-pattern
                Element servletMapping = (Element) rootElement.selectSingleNode("/web-app/servlet-mapping[servlet-name='" + servletName + "']");
                String urlPattern = servletMapping.selectSingleNode("url-pattern").getStringValue();// /lagou
                System.out.println(host + urlPattern);
                servletMap.put(host + urlPattern,(HttpServlet) Class.forName(servletClass).newInstance());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return servletMap;
    }

    /**
     * 解析本项目中的server.xml文件
     * @return
     */
    public static PortAndServlet loadServerConf(){
        Map<String,HttpServlet> servletMap = new HashMap<>();
        Integer port = 8080;//默认值
        InputStream inputStream = CommonUtil.class.getClassLoader().getResourceAsStream("server.xml");
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(inputStream);
            Element rootElement = document.getRootElement();
            port = Integer.parseInt(rootElement.selectSingleNode("Service").selectSingleNode("Connector").valueOf("@port"));
            List<Element> hosts = rootElement.selectNodes("//Host");
            for (Element host : hosts) {
                String servletMapKey = "http://"+host.attributeValue("name")+":"+port;
                String appBase = host.attributeValue("appBase")+"/WEB_INFO/web.xml";
                File webxml = new File(appBase);
                InputStream fileInputStream = new FileInputStream(webxml);
                servletMap = loadServlet(fileInputStream,servletMapKey);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new PortAndServlet(port,servletMap);
    }
}
